#ifndef STRUCTURE__H
#define STRUCTURE__H

#include "../common.h"

int insert_var(const char * );
int insert_label(const char * );

typedef struct map_unit_ * map_unit;
struct map_unit_{
    uLL key;
    void * pointer;
};
struct map_{
    List entry[10];
};
typedef struct map_ * MAP;
MAP new_map();
void ** map_insert(MAP, uLL);
void ** map_find(MAP, uLL);
void map_erase(MAP, uLL);
void map_clear(MAP);
void map_cpy(MAP,MAP);
void set_insert(MAP,uLL);
bool set_find(MAP, uLL);
#define FOR_MAP_2(iter,self,nxt)\
for(int _id=0;_id<10; _id++)\
FOR_LIST_2(iter,self->entry[_id],nxt)
#define FOR_MAP(iter,self) FOR_MAP_2(iter,self,iter->nxt)

#define MAP_KEY(iter) (LIST_GET(iter, map_unit)->key)
#define MAP_VAL(iter,type) ((type) LIST_GET(iter, map_unit)->pointer )
#define MAP_CLEAR(self) if(self) map_clear(self),free(self),self=NULL

typedef struct function_ function;
struct function_{
    const char * name;
    int insert_status;
    List BBlist;//BB
    List param;//uLL
};

typedef struct BB_ BB;
struct BB_{
    List entry,exit;//BB
    List statement;//Op
    MAP mp1,mp2;
    BB * nxt0;
};

enum OP_ID{
    OP_LABEL,OP_FUNC,
    OP_CALL,OP_READ,OP_ASSIGN,
    OP_ADD,OP_SUB,OP_MUL,OP_DIV,
    OP_RET,OP_ARG,OP_WRITE,
    OP_IF,OP_GOTO,OP_DEC,
};
typedef struct operation_ operation;
#define CONSTANT_ID 114514
typedef union{
    struct{
        int no;
        int addr_status;
    }var;
    int label;
}Operand;
struct operation_{
    int op;uLL info;
    Operand x,y,z;
};
#define FREE_OPERATION(o) \
do{\
    free(o);\
} while (0)


extern List function_list;
extern bool var_mark[20000];
#endif
